package com.schoolegard.services.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.schoolegard.services.LocationService;
import com.schoolgard.dao.DeviceDao;
import com.schoolgard.dao.LocationDao;
import com.schoolgard.dao.OrganizationDao;
import com.schoolgard.dao.RegionsDao;
import com.schoolgard.entitybean.Device;
import com.schoolgard.entitybean.Location;
import com.schoolgard.entitybean.Organization;
import com.schoolgard.entitybean.Regions;

public class LocationServiceImpl implements LocationService {
	
	private LocationDao locationDao;
	private DeviceDao deviceDao;
	private RegionsDao regionsDao;
	private OrganizationDao organizationDao;
	public void setLocationDao(LocationDao locationDao) {
		this.locationDao = locationDao;
	}
	public void setDeviceDao(DeviceDao deviceDao) {
		this.deviceDao = deviceDao;
	}
	public void setRegionsDao(RegionsDao regionsDao) {
		this.regionsDao = regionsDao;
	}
	public void setOrganizationDao(OrganizationDao organizationDao) {
		this.organizationDao = organizationDao;
	}
	@Override
	public Location findLocaBySPNodeId(String nodeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Location.class);
		String refId = null;
		if(nodeId.indexOf("re_") != -1){
			criteria.add(Restrictions.eq("type", "REGIONS"));
			refId = nodeId.replace("re_", "");
		}else if(nodeId.indexOf("or_") != -1){
			criteria.add(Restrictions.eq("type", "ORGANIZATION"));
			refId = nodeId.replace("or_", "");
		}else if(nodeId.indexOf("de_") != -1){
			criteria.add(Restrictions.eq("type", "DEVICE"));
			refId = nodeId.replace("de_", "");
		}
		criteria.add(Restrictions.eq("refId", refId));
		List<Location> ls = locationDao.findByCriteria(criteria);
		return (ls != null && ls.size() > 0)?ls.get(0):null;
	}
	@Override
	public void saveOrUpdate(Location loca) {
		locationDao.createOrModify(loca);
	}
	@Override
	public Location findByRefId(String refId) {
		 DetachedCriteria criteria = DetachedCriteria.forClass(Location.class);
	     criteria.add(Restrictions.eq("refId", refId));
	     List<Location> locas = locationDao.findByCriteria(criteria);
	     if(locas != null && locas.size() > 0){
	    	 return locas.get(0);
	     }
	     return null;
	}
	@Override
	public List<Location> findLocation() {
		List<Location> locls = locationDao.findAll();
		List<Location> locnew = new ArrayList<Location>();
		for (Location location : locls) {
			if("REGIONS".equals(location.getType())){
				Regions reg = regionsDao.findById(Long.valueOf(location.getRefId()));
				if(reg != null) locnew.add(location);
			}else if("DEVICE".equals(location.getType())){
				Device de = deviceDao.findById(location.getRefId());
				if(de != null) locnew.add(location);
			}else if("ORGANIZATION".equals(location.getType())){
				Organization org = organizationDao.findById(Long.valueOf(location.getRefId()));
				if(org != null) locnew.add(location);
			}
		}
		return locnew;
	}
	@Override
	public void delete(Long id) {
		locationDao.remove(locationDao.findById(id));
	}

}
